package dao.impl;

import cqcet.aibd.soft.ObjectUtil;
import dao.GoodsDAO;
import model.Goods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

/**
 * @Description: TODO
 * @author: april
 * @date: 2021年11月25日 21:57
 */
public class GoodsDAOImpl implements GoodsDAO {
    @Override
    public List<Goods> getGoodsList() throws Exception {
        String sql = "select * from goods";
        return new ObjectUtil<Goods>().getList(sql, Goods.class);
    }

    @Override
    public Goods getGoodsById(String id) {
        String sql = "select * from goods where id=?";
        return new ObjectUtil<Goods>().getOne(sql, Goods.class, id);
    }

    @Override
    public int addGoods(Goods goods) {
        String sql = "insert into goods (id, createTime, isDel, name, price, number, brand) values (?, ?, ?, ?, ?, ?, ?)";
        return new ObjectUtil<Goods>().add(sql, goods.getId(), goods.getCreateTime(), goods.getIsDel(), goods.getName(),
                goods.getPrice(), goods.getNumber(), goods.getBrand());
    }

    @Override
    public int updateGoods(String id, Goods goods) {
        String sql = "update goods set name=?,brand=?,price=?,number=?,createTime=?,deleteTime=?," +
                "isDel=? where id=?";
        return new ObjectUtil<Goods>().update(sql, goods.getName(), goods.getBrand(),
                goods.getPrice(), goods.getNumber(), goods.getCreateTime(), goods.getDeleteTime(),
                goods.getIsDel(), id);
    }

    @Override
    public int deleteGoods(String id) {
        String sql = "delete from goods where id=?";
        return new ObjectUtil<Goods>().delete(sql, id);
    }

    @Override
    public List<Goods> getGoodsByName(String name) {
        String sql = "select * from goods where name like ?";
        return new ObjectUtil<Goods>().getList(sql, Goods.class, "%" + name + "%");
    }

    @Override
    public int updateGoodsStock(Connection con, Goods goods, Integer flag, Integer num) throws Exception{
        String str = "number = number ";
        switch (flag) {
            case 1:
                str += "+ ? ";
                break;
            case 2:
                str += "- ?";
                break;
            default:
                str += "+ ?";
        }

        PreparedStatement ps = null;

        String sql = "update goods set " + str + " where id = ?";
        ps = con.prepareStatement(sql);
        ps.setDouble(1, num);
        ps.setString(2, goods.getId());
        return ps.executeUpdate();


    }


    @Override
    public int updateGoodsStock(Goods goods, Integer flag, Integer num) {
        String str = "number = number ";
        switch (flag) {
            case 1:
                str += "+ ? ";
                break;
            case 2:
                str += "- ?";
                break;
            default:
                str += "+ ?";
        }
        String sql = "update goods set " + str + " where id = ?";
        return new ObjectUtil<Goods>().update(sql, num, goods.getId());
    }

}
